package org.zjx;

import lombok.SneakyThrows;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.zjx.service.AssociationServer;
import org.zjx.service.BuyItemServer;
import org.zjx.service.NpcService;
import org.zjx.service.impl.AssociationServerImpl;
import org.zjx.service.impl.BuyItemServerImpl;
import org.zjx.service.impl.NpcServiceImpl;

public class Main {
    @SneakyThrows
    public static void main(String[] args) {
        ApplicationConfig applicationConfig = new ApplicationConfig("sample-provider");
        applicationConfig.setSerializeCheckStatus("DISABLE");

        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(23136);
        protocolConfig.setSerialization("fastjson2"); // 强制使用FastJSON序列化

        RegistryConfig registryConfig = new RegistryConfig(RegistryConfig.NO_AVAILABLE);

        // 2. 暴露 UserService
        ServiceConfig<NpcService> routeServiceServiceConfig = new ServiceConfig<>();
        routeServiceServiceConfig.setInterface(NpcService.class);
        routeServiceServiceConfig.setRef(new NpcServiceImpl());
        routeServiceServiceConfig.setRegistry(registryConfig);
        routeServiceServiceConfig.setProtocol(protocolConfig);
        routeServiceServiceConfig.setApplication(applicationConfig);
        routeServiceServiceConfig.export();

        // 3. 暴露 BuyItemServer
        ServiceConfig<BuyItemServer> buyItemServerServiceConfig = new ServiceConfig<>();
        buyItemServerServiceConfig.setInterface(BuyItemServer.class);
        buyItemServerServiceConfig.setRef(new BuyItemServerImpl());
        buyItemServerServiceConfig.setRegistry(registryConfig);
        buyItemServerServiceConfig.setProtocol(protocolConfig);
        buyItemServerServiceConfig.setApplication(applicationConfig);
        buyItemServerServiceConfig.export();


        // 3. 暴露 BuyItemServer
        ServiceConfig<AssociationServer> associationServerServiceConfig = new ServiceConfig<>();
        associationServerServiceConfig.setInterface(AssociationServer.class);
        associationServerServiceConfig.setRef(new AssociationServerImpl());
        associationServerServiceConfig.setRegistry(registryConfig);
        associationServerServiceConfig.setProtocol(protocolConfig);
        associationServerServiceConfig.setApplication(applicationConfig);
        associationServerServiceConfig.export();

        System.out.println("ScreenService 和 NpcService 已暴露,使用序列化协议: " + protocolConfig.getSerialization());

        System.in.read(); // 阻塞主线程
    }
}